/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.user.object.User;
import etcore.webapp.grouptalk.user.service.UserService;

/**
 * This action executes the command of logout
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class LogoutAction extends AbstractAction {

	private static final long serialVersionUID = 4116011649135198709L;
	// validate result
	public static final int LOGGED_IN = 1;
	public static final int NOT_LOGGED_IN = 2;
	public static final int INVALID_CMD = 3;
	// parameter constants

	public static final String CMD_VALUE = "userLogout";

	// utility constants

	// forward pages
	public static final String START_PAGE = "/";
	public static final String FAILURE_PAGE = "/error.jsp";

	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_VALUE.equals(cmd)) {
			HttpSession session = request.getSession(true);
			User sessionUser = (User) session
					.getAttribute(Messages.SESSION_USER);
			if (sessionUser == null) {
				return NOT_LOGGED_IN;

			} else {
				return LOGGED_IN;
			}
		} else {
			return INVALID_CMD;
		}

	}

	@Override
	public String getForward(HttpServletRequest request, int validationResult) {
		if (NOT_LOGGED_IN == validationResult) {

			return FAILURE_PAGE;
		} else
			return START_PAGE;
	}

	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession(true);
		if (LOGGED_IN == validationResult) {

			User sessionUser = (User) session
					.getAttribute(Messages.SESSION_USER);
			UserService userService = new UserService();
			User user = userService.findById(sessionUser.getId());
			if (user != null) {
				user.setIsOnline(new Integer(0));
				userService.merge(user);
			}

		} else if (NOT_LOGGED_IN == validationResult) {
			String lang = (String) session.getAttribute(Messages.LANG);
			String msg = null;
			if (lang != null && lang.equals(Messages.LANG_EN)) {
				msg = Messages.getELabel(Messages.MSG_NOT_LOGGED);
			} else {
				msg = Messages.getLabel(Messages.MSG_NOT_LOGGED);
			}
			request.setAttribute(Messages.MSG, msg);
		}
		session.invalidate();

	}

	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

}
